/**
 * 
 */
package com.asset.salesforce.controller.pages;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.City;
import com.asset.salesforce.domain.Country;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.enums.INDUSTRY;
import com.asset.salesforce.enums.TYPE;
import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.CityService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.EmployeeService;

// TODO: Auto-generated Javadoc
/**
 *
 * @author Hisham Mounir
 * this controller class is used to handle the account pages
 */
@Controller
public class AccountController {

	/** The account service. */
	@Autowired
	private AccountService accountService;
	
	/** The employee service. */
	@Autowired
	private EmployeeService employeeService;
	
	/** The country service. */
	@Autowired
	private CountryService countryService;
	
	/** The city service. */
	@Autowired
	private CityService cityService;
	
	/**
	 * Gets the accounts page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @return the accounts page
	 */
	@RequestMapping(value = "/accounts", method = RequestMethod.GET)
	public String getAccountsPage(HttpSession session, Model model,HttpServletResponse response) {
		if (null == session.getAttribute("userDetails")) {return "redirect:login";}
		return "accountsPage";
	}
	
	/**
	 * Gets the new account page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @param request the request
	 * @return the new account page
	 */
	@RequestMapping(value = "/newAccount", method = RequestMethod.GET)
	public String getCreateAccountsPage(HttpSession session, Model model,HttpServletResponse response,HttpServletRequest request) {
		if (SecurityUtils.getSubject().hasRole("accountAdd")){
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:login";
			}
			Employee userDetails = (Employee)session.getAttribute("userDetails");
			INDUSTRY [] industries = INDUSTRY.values();
			TYPE [] types = TYPE.values();
			model.addAttribute("industries", industries);
			model.addAttribute("types", types);
			model.addAttribute("userDetails",userDetails);
			List<Country> countries = countryService.getCountriesOrderById();
			model.addAttribute("countries",countries);
			model.addAttribute("accountDTO",new Account());
			return "createAccountPage";
			}
			else{
				return "accessDenied";
			}
	}
	
	/**
	 * Post new account.
	 *
	 * @param model the model
	 * @param session the session
	 * @param accountForm the account form
	 * @param result the result
	 * @return the string
	 */
	@RequestMapping(value="/newAccount",method=RequestMethod.POST)
	public String postNewAccount(Model model,HttpSession session,@Valid @ModelAttribute("accountDTO") Account accountForm,BindingResult result){
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		Account account = new Account();
		account.setAccountCreator(userDetails);
		if(accountForm.getAccountName() != null && !accountForm.getAccountName().equalsIgnoreCase("")){
			account.setAccountName(accountForm.getAccountName());
		}
		if(accountForm.getCompanyName() != null && !accountForm.getCompanyName().equalsIgnoreCase("")){
			account.setCompanyName(accountForm.getCompanyName());
		}
		if(accountForm.getAnnualRevenue() != null){
			account.setAnnualRevenue(accountForm.getAnnualRevenue());
		}
		if(accountForm.getCity()!=null && !accountForm.getCity().getId().equals(0)){
			account.setCity(cityService.getById(accountForm.getCity().getId()));
		}
		if(accountForm.getCountry() != null){
			account.setCountry(countryService.getById((accountForm.getCountry().getId())));
		}
		if(accountForm.getDescription() != null && !accountForm.getDescription().equalsIgnoreCase("")){
			account.setDescription(accountForm.getDescription());
		}
		if(accountForm.getEmail() != null && !accountForm.getEmail().equalsIgnoreCase("")){
			account.setEmail(accountForm.getEmail());
		}
		if(accountForm.getEmpNum() != null){
			account.setEmpNum(accountForm.getEmpNum());
		}
		if(accountForm.getFax() != null && !accountForm.getFax().equalsIgnoreCase("")){
			account.setFax(accountForm.getFax());
		}
		if(accountForm.getIndustry() != null){
			INDUSTRY[] industries=	INDUSTRY.values();
			account.setIndustry(industries[Integer.valueOf(accountForm.getIndustry())].getName());
		}
		if(accountForm.getPhone() != null && !accountForm.getPhone().equalsIgnoreCase("")){
			account.setPhone(accountForm.getPhone());
		}
		if(accountForm.getMobile() != null && !accountForm.getMobile().equalsIgnoreCase("")){
			account.setMobile(accountForm.getMobile());
		}
		if(accountForm.getState() != null && !accountForm.getState().equalsIgnoreCase("")){
			account.setState(accountForm.getState());
		}
		if(accountForm.getStreet() != null && !accountForm.getStreet().equalsIgnoreCase("")){
			account.setStreet(accountForm.getStreet());
		}
		if(accountForm.getType() != null){
			TYPE[] types =TYPE.values();
			account.setType(types[Integer.valueOf(accountForm.getType())].getName());
		}
		if(accountForm.getWebSite() != null && !accountForm.getWebSite().equalsIgnoreCase("")){
			account.setWebSite(accountForm.getWebSite());
		}
		if(accountForm.getZipCode() != null && !accountForm.getZipCode().equalsIgnoreCase("")){
			account.setZipCode(accountForm.getZipCode());
		}
		account.setIsDeleted(false);
		Date date = new Date();
		account.setDateCreated(date);
		account.setDateModified(date);
		accountService.add(account);
		return "redirect:accounts";
	}
	
	/**
	 * Gets the edit account page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @param request the request
	 * @param id the id
	 * @return the edit account page
	 */
	@RequestMapping(value = "/editAccount", method = RequestMethod.GET)
	public String getEditAccountsPage(HttpSession session, Model model,HttpServletResponse response,HttpServletRequest request,@RequestParam Integer id) {
		if (SecurityUtils.getSubject().hasRole("accountEdit")){
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:login";
			}
			Employee userDetails = (Employee)session.getAttribute("userDetails");
			INDUSTRY [] industries = INDUSTRY.values();
			TYPE [] types = TYPE.values();
			model.addAttribute("industries", industries);
			model.addAttribute("types", types);
			model.addAttribute("userDetails",userDetails);
			List<Country> countries = countryService.getCountriesOrderById();
			model.addAttribute("countries",countries);
			Account account = accountService.getById(id);
			model.addAttribute("accountDTO",account);
			if(account.getCountry()!=null){
				List<City>cities = cityService.getAllCityByCountryId(account.getCountry().getId());
				model.addAttribute("cities",cities);
			}
			return "editAccountPage";
			}
			else{
				return "accessDenied";
			}
	}
	
	/**
	 * Post edit account.
	 *
	 * @param model the model
	 * @param session the session
	 * @param accountForm the account form
	 * @param result the result
	 * @return the string
	 */
	@RequestMapping(value="/editAccount",method=RequestMethod.POST)
	public String postEditAccount(Model model,HttpSession session,
			@Valid @ModelAttribute("accountForm") Account accountForm,BindingResult result){
		Employee userDetails = (Employee)session.getAttribute("userDetails");

		Account account = accountService.getById(accountForm.getId());
		account.setAccountCreator(userDetails);

		if(accountForm.getAccountName() != null && !accountForm.getEmail().equalsIgnoreCase("")){
			account.setAccountName(accountForm.getAccountName());
		}
		if(accountForm.getCompanyName() != null && !accountForm.getCompanyName().equalsIgnoreCase("")){
			account.setCompanyName(accountForm.getCompanyName());
		}
		if(accountForm.getAnnualRevenue() != null){
			account.setAnnualRevenue(accountForm.getAnnualRevenue());
		}
		if(accountForm.getCity()!=null && !accountForm.getCity().getId().equals(0)){
			account.setCity(cityService.getById(accountForm.getCity().getId()));
		}
		else{
			account.setCity(null);
		}
		if(accountForm.getCountry() != null){
			account.setCountry(countryService.getById((accountForm.getCountry().getId())));
		}
		if(accountForm.getDescription() != null && !accountForm.getDescription().equalsIgnoreCase("")){
			account.setDescription(accountForm.getDescription());
		}
		if(accountForm.getEmail() != null && !accountForm.getEmail().equalsIgnoreCase("")){
			account.setEmail(accountForm.getEmail());
		}
		if(accountForm.getEmpNum() != null){
			account.setEmpNum(accountForm.getEmpNum());
		}
		if(accountForm.getFax() != null && !accountForm.getFax().equalsIgnoreCase("")){
			account.setFax(accountForm.getFax());
		}
		if(accountForm.getIndustry() != null){
			INDUSTRY[] industries=	INDUSTRY.values();
			account.setIndustry(industries[Integer.valueOf(accountForm.getIndustry())].getName());
		}
		if(accountForm.getPhone() != null && !accountForm.getPhone().equalsIgnoreCase("")){
			account.setPhone(accountForm.getPhone());
		}
		if(accountForm.getMobile() != null && !accountForm.getMobile().equalsIgnoreCase("")){
			account.setMobile(accountForm.getMobile());
		}
		if(accountForm.getState() != null && !accountForm.getState().equalsIgnoreCase("")){
			account.setState(accountForm.getState());
		}
		if(accountForm.getStreet() != null && !accountForm.getStreet().equalsIgnoreCase("")){
			account.setStreet(accountForm.getStreet());
		}
		if(accountForm.getType() != null){
			TYPE[] types =TYPE.values();
			account.setType(types[Integer.valueOf(accountForm.getType())].getName());
		}
		if(accountForm.getWebSite() != null && !accountForm.getWebSite().equalsIgnoreCase("")){
			account.setWebSite(accountForm.getWebSite());
		}
		if(accountForm.getZipCode() != null && !accountForm.getZipCode().equalsIgnoreCase("")){
			account.setZipCode(accountForm.getZipCode());
		}
		account.setIsDeleted(false);
		account.setDateModified(new Date());
		accountService.saveOrupdate(account);
		return"redirect:accounts";
	}
	
	/**
	 * Gets the view accounts page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @param request the request
	 * @param id the id
	 * @return the view accounts page
	 */
	@RequestMapping(value = "/viewAccount", method = RequestMethod.GET)
	public String getViewAccountsPage(HttpSession session, Model model,HttpServletResponse response,HttpServletRequest request,@RequestParam Integer id) {
		if (SecurityUtils.getSubject().hasRole("accountView")){
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:login";
			}
			Account account = accountService.getById(id);
			model.addAttribute("accountDTO",account);
			return "viewAccountPage";
			}
			else{
				return "accessDenied";
			}
	}
}
